package com.bbva.tasks;


import com.microstrategy.utils.StringUtils;
import com.microstrategy.utils.log.Level;
import com.microstrategy.web.app.tasks.BeanTaskRequestContext;
import com.microstrategy.web.app.tasks.ReportExecutionTask;
import com.microstrategy.web.tasks.TaskException;
import com.microstrategy.web.tasks.TaskParameterMetadata;
import com.microstrategy.web.tasks.TaskRequestMalformedException;
public class CustomReportExecutionTask extends ReportExecutionTask {

	private String sVersion="DHLM 1.8 2013-01-10";

	public CustomReportExecutionTask() {
		super();
		String FUNCTION_NAME = "CustomReportExecutionTask";
		Log.logger.logp(Level.INFO, CLASS_NAME, FUNCTION_NAME , "Start Custom Report Execution" + sVersion);
	}
	protected void handleEvent(BeanTaskRequestContext beantaskrequestcontext)
    throws TaskException
    {
		String FUNCTION_NAME ="handleEvent";
		Log.logger.logp(Level.INFO, CLASS_NAME, FUNCTION_NAME  , " Custom Report Execution");
		try
		{
			String sValPromptAns= resolvePrompts(beantaskrequestcontext.getRequestKeys().getValue(CUSTOM_VALUE_PROMPT_KEY));
			if(sValPromptAns != null) 
				beantaskrequestcontext.getRequestKeys().add("valuePromptAnswers",sValPromptAns);
			sValPromptAns=null;
			super.handleEvent(beantaskrequestcontext);
	
		}
		catch(Exception exception)
		{
			throw new TaskRequestMalformedException((new StringBuilder()).append("The task '").append(getID()).append("' could not be completed due to an event handling error: ").append(exception.getMessage()).toString());
		}
    }
	
	private String resolvePrompts(String customPromptValue) {
		String FUNCTION_NAME ="handleEvent";
		Log.logger.logp(Level.INFO, CLASS_NAME, FUNCTION_NAME  , " Custom Report Execution");
		String aCustomAnswers[];
		String result=null;
		if(customPromptValue!=null){
			aCustomAnswers = StringUtils.split(customPromptValue,";");
			String aFAnswer[] = StringUtils.split(aCustomAnswers[0],":");
			int iPromptPos = Integer.parseInt(aFAnswer[0]);
			int iPromptLastPos = Integer.parseInt(aFAnswer[0]);
			String sPromptAnswer =aFAnswer[1];
			result=repeat("^",iPromptPos)+sPromptAnswer;
			for(int i=1;i<aCustomAnswers.length;i++){
				String aAnswer[] = StringUtils.split(aCustomAnswers[i],":");
				iPromptPos = Integer.parseInt(aAnswer[0]);
				sPromptAnswer =aAnswer[1];
				int numberSeparators=(iPromptPos-iPromptLastPos);
    			result+=repeat("^",numberSeparators)+sPromptAnswer;
    			iPromptLastPos = iPromptPos;
    			aAnswer[0]=null;
    			aAnswer[1]=null;
    			aAnswer=null;
    		}
			aFAnswer=null;
			sPromptAnswer=null;
		}
		//System.out.println("customValuePrompt:"+result);
		return result;		
	}
	
	public static String repeat(String s, int times) {
		   if (times <= 0) return "";
		   else if (times % 2 == 0) return repeat(s+s, times/2);
		   else return s + repeat(s+s, times/2);
		}

	protected void registerParameterMetadata(TaskParameterMetadata taskparametermetadata)
    {
    	super.registerParameterMetadata(taskparametermetadata);
    	if(taskparametermetadata.getName().equals(REPORT_STYLE))
            reportStyle = taskparametermetadata;
    }

	 private  String CUSTOM_VALUE_PROMPT_KEY = "custValuePromptAnswers";
	 private String REPORT_STYLE = "styleName";
	 private String CLASS_NAME = "com.bbva.tasks.CustomReportExecutionTask";
	 @SuppressWarnings("unused")
	 private TaskParameterMetadata reportStyle;
	 @SuppressWarnings("unused")
	 private TaskParameterMetadata repeatTotalsParam;
	
}